package de.jare.photoswapper;

import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Writer;
import java.io.OutputStreamWriter; 
import java.net.Socket; 
import java.net.UnknownHostException;


import android.util.Log;

public class HttpHandler {
	private String host = "swap.ganzbunt.de";
	public String AndroidID;
	
	public void UploadPicture(byte[] data) {
		try {
			if (AndroidID == null)
			{
				AndroidID = "123456789";
			}
			
			Log.d("PS", "HttpHandler.UploadPicture(): Connecting to " + host);
			
			Socket socket = new java.net.Socket(host, 80);
			
			DataOutputStream  os = new DataOutputStream(socket.getOutputStream());
			Writer wr = new OutputStreamWriter(os);
			
			String multipart2;
			multipart2 = "-----------------------------29772313742745\r\n"
				+ "Content-Disposition: form-data; name=\"photoswapper\"; filename=\"upload.jpg\"\r\n"
				+ "Content-Type: image/jpeg\r\n\r\n";
			
			String multipart1;
			multipart1 = "-----------------------------29772313742745\r\n"
				+ "Content-Disposition: form-data; name=\"MAX_FILE_SIZE\"\r\n"
				+ "\r\n5000000\r\n";
			
			String trail;
			
			trail = "\r\n-----------------------------29772313742745--\r\n";
			
			String header;
			header = "POST /test.php HTTP/1.1\r\n"
				+ "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" + "Referer: http://localhost\r\n"
				+ "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n"
				+ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
				+ "Content-Type: multipart/form-data; boundary=---------------------------29772313742745\r\n"
				+ "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)\r\n"
				+ "Host: " + host + "\r\n"
				+ "Content-length: " + ((int)data.length + (int)multipart1.length() + (int)multipart2.length() + (int)trail.length()) + "\r\n"
				+ "Keep-Alive: 300\r\n"
				+ "Connection: Keep-Alive\r\n"
				+ "Pragma: no-cache\r\n"
				+ "AndroidId: " + AndroidID + "\r\n"
				+ "\r\n";
			
			wr.write(header);
			wr.write(multipart1);
			wr.write(multipart2);
			wr.flush();
			
			os.write(data);
			os.flush();
			
			wr.write(trail);
			wr.flush();
			
			Log.d("PS", "HttpHandler.UploadPicture(): flushing");
			
			
		} catch (UnknownHostException e) {
			Log.d("PS", "HttpHandler.UploadPicture(): IOException " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("PS", "HttpHandler.UploadPicture(): UnknownHostException " + e.getMessage());
			e.printStackTrace();
		}
	}
	
	public void ReceivePicture() {
		try {
			if (AndroidID == null)
			{
				AndroidID = "123456789";
			}
			
			Log.d("PS", "HttpHandler.ReceivePicture(): Connecting to " + host);
			
			Socket socket = new java.net.Socket(host, 80);
			
			DataOutputStream  os = new DataOutputStream(socket.getOutputStream());
			Writer wr = new OutputStreamWriter(os);
			
			String header;
			header = "GET /get.php HTTP/1.1\r\n"
				+ "Accept: image/jpeg\r\n" + "Referer: http://localhost\r\n"
				+ "Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3\r\n"
				+ "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"
				+ "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; de; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 (.NET CLR 3.5.30729)\r\n"
				+ "Host: " + host + "\r\n"
				+ "Keep-Alive: 300\r\n"
				+ "Connection: Keep-Alive\r\n"
				+ "Pragma: no-cache\r\n"
				+ "AndroidId: " + AndroidID + "\r\n"
				+ "\r\n";
			
			wr.write(header);
			wr.flush();
			
			
			Log.d("PS", "HttpHandler.ReceivePicture(): flushing");
			
			
		} catch (UnknownHostException e) {
			Log.d("PS", "HttpHandler.UploadPicture(): IOException " + e.getMessage());
			e.printStackTrace();
		} catch (IOException e) {
			Log.d("PS", "HttpHandler.UploadPicture(): UnknownHostException " + e.getMessage());
			e.printStackTrace();
		}
	}
}
